package com.geek.service.impl;

import com.geek.bean.vo.RegisterInfoVo;
import com.geek.mapper.MarketOrderMapper;
import com.geek.mapper.MarketUserMapper;
import com.geek.model.MarketOrder;
import com.geek.model.MarketOrderExample;
import com.geek.model.MarketUser;
import com.geek.model.MarketUserExample;
import com.geek.service.UserService;
import com.geek.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    MarketOrderMapper orderMapper;


    @Override
    public Integer getUnpaid(Integer userId) {
        // 查询待支付的订单数量
        MarketOrderExample example = new MarketOrderExample();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andUserIdEqualTo(userId)
                .andOrderStatusEqualTo((short) 101);
        Integer unpidCount = (int) orderMapper.countByExample(example);
        return unpidCount;
    }

    @Override
    public Integer getUnrecv(Integer userId) {
        // 查询待收货的订单数量
        MarketOrderExample example = new MarketOrderExample();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andUserIdEqualTo(userId)
                .andOrderStatusEqualTo((short) 301);
        Integer unrecvCount = (int) orderMapper.countByExample(example);
        return unrecvCount;
    }

    @Override
    public Integer getUnship(Integer userId) {
        // 查询待发货的订单数量
        MarketOrderExample example = new MarketOrderExample();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andUserIdEqualTo(userId)
                .andOrderStatusEqualTo((short) 201);
        Integer unshipCount = (int) orderMapper.countByExample(example);
        return unshipCount;
    }

    @Override
    public Short getUncomment(Integer userId) {
        // 查询待评价的订单商品数量
        MarketOrderExample example = new MarketOrderExample();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andUserIdEqualTo(userId)
                .andCommentsNotEqualTo((short) 0);
        List<MarketOrder> marketOrderList = orderMapper.selectByExample(example);
        if (marketOrderList == null || marketOrderList.size() == 0) {
            return 0;
        }
        short uncomment = marketOrderList.stream().map(marketOrder -> marketOrder.getComments()).reduce((a, b) -> (short) (a + b)).get();
        return uncomment;
    }

    @Override
    public MarketUser findByName(String username) {
        MarketUserExample example = new MarketUserExample();
        example.createCriteria().andUsernameEqualTo(username)
                .andStatusEqualTo((byte) 0)
                .andDeletedEqualTo(false);
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        return marketUsers != null && marketUsers.size() == 1 ? marketUsers.get(0) : null;
    }

    @Override
    public MarketUser findById(Integer userId) {
        MarketUserExample example = new MarketUserExample();
        example.createCriteria().andIdEqualTo(userId).andDeletedEqualTo(false);
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        return marketUsers != null && marketUsers.size() == 1 ? marketUsers.get(0) : null;
    }

    /**
     * 注册用户
     * @param registerInfoVo  注册信息
     * @return 返回1表示手机号已注册，返回2表示用户名已存在，返回0表示注册成功
     */
    @Override
    public int create(RegisterInfoVo registerInfoVo) {
        String mobile = registerInfoVo.getMobile();
        String username = registerInfoVo.getUsername();
        MarketUserExample example = new MarketUserExample();
        example.createCriteria()
                .andMobileEqualTo(mobile)
                .andDeletedEqualTo(false);
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        if (marketUsers != null && marketUsers.size() != 0) {
            //手机号已注册
            return 1;
        }

        example.clear();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andUsernameEqualTo(username);
        List<MarketUser> usernames = userMapper.selectByExample(example);
        if (usernames != null && usernames.size() > 0) {
            //用户名已存在
            return 2;
        }

        //用MD5加密对密码进行加密
        String password = registerInfoVo.getPassword();
        String encryptPwd = Md5Util.getMd5(password,registerInfoVo.getUsername());
        //注册用户信息，向market_user表插入数据
        MarketUser user = MarketUser.builder()
                .addTime(new Date())
                .mobile(mobile)
                .password(encryptPwd)
                .username(username)
                .weixinOpenid(registerInfoVo.getWxCode())
                .userLevel((byte) 0)
                .updateTime(new Date())
                .deleted(false)
                .nickname(username)
                .avatar("https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64")
                .build();

        userMapper.insertSelective(user);
        return 0;
    }

    @Override
    public int findByMobile(String mobile) {
        MarketUserExample example = new MarketUserExample();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andMobileEqualTo(mobile);
        List<MarketUser> users = userMapper.selectByExample(example);
        return users != null && users.size() == 1 ? 1 : 0;
    }

    @Override
    public void modifyPassword(String mobile, String password) {

        MarketUserExample example = new MarketUserExample();
        example.createCriteria()
                .andMobileEqualTo(mobile)
                .andDeletedEqualTo(false);
        //查询用户名
        MarketUser marketUser = userMapper.selectByExample(example).get(0);
        String encryptPwd = Md5Util.getMd5(password, marketUser.getUsername());
        MarketUser user = MarketUser.builder().password(encryptPwd).updateTime(new Date()).build();
        userMapper.updateByExampleSelective(user,example);
    }

    @Override
    public void updateLoginTime(Integer userId) {
        MarketUserExample example = new MarketUserExample();
        example.createCriteria()
                .andIdEqualTo(userId)
                .andDeletedEqualTo(false);
        MarketUser user = MarketUser.builder().lastLoginTime(new Date()).build();
        userMapper.updateByExampleSelective(user,example);
    }
}
